package kr.hkit.androidcomplete02.ch17;

import kr.hyunist.androidcomplete01.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class CommActivity extends Activity {
	TextView tv;
	final static int REQ_CODE = 0;
	final static int CAL_REQ_CODE = 1;	//계산 액티비티 req code
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch17_commactivity);
		
		tv = (TextView) findViewById(R.id.ch17_tv);
	}
	
	public void mOnClick(View v){
		if(v.getId() == R.id.ch17_btnedit){
			Intent intent = new Intent(CommActivity.this, ActEdit.class);
			intent.putExtra("TextIn", tv.getText().toString());
			
			startActivityForResult(intent, REQ_CODE);
			
		}
		
		if (v.getId()==R.id.ch17_calbtn){
			Intent intent = new Intent(CommActivity.this, CalAct.class);
			intent.putExtra("leftValue", 3);
			intent.putExtra("rightValue", 4);
			
			startActivityForResult(intent, CAL_REQ_CODE);
		}
	}
	
	//수행된 subActivity가 setResult()를 실행할때 호출
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode){
		case REQ_CODE:
			if(resultCode == RESULT_OK){
				tv.setText(data.getStringExtra("TextOut"));
			} else { 
				tv.setText("Cancel");
			}
			break;
		case CAL_REQ_CODE:
			if(resultCode == RESULT_OK){
				tv.setText(data.getStringExtra("Result"));
			}
			break;
		}
	
	}
}
